package com.example.myblog1_0.service.Impl;

import com.example.myblog1_0.mapper.ArticleMapper;
import com.example.myblog1_0.model.Article;
import com.example.myblog1_0.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: xc
 * @Date: 2018/12/15 18:49
 * @Description:
 **/
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Override
    public Article findArticleByTitle(String title) {
        return articleMapper.findArticleByTitle(title);
    }

    @Override
    public Article findArticleByTime(String time) {
        return articleMapper.findArticleByTime(time);
    }

    @Override
    public boolean insetArticle(Article article) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String writeTime = sdf.format(new Date());

        article.setWriteTime(writeTime);
        return articleMapper.insetArticle(article);
    }

    @Override
    public List<Article> findAllArticleDesc() {
        List<Article> articles = articleMapper.findAllArticleDesc();
        return articles;
    }

    @Override
    public Map<String, String> findArticleById(String id) {
        String title = articleMapper.findArticleById(id);
        Map<String,String> articleMap = new HashMap<>();
        articleMap.put("articleTitle",title);
        return articleMap;
    }

    @Override
    public JSONObject getArticleByArticleId(String id) {
        Article findArticle = articleMapper.getArticleByArticleId(id);
        JSONObject date = new JSONObject();
        date.put("status",200);
        date.put("articleTitle",findArticle.getTitle());
        date.put("publishDate",findArticle.getWriteTime());
        date.put("articleContent",findArticle.getArticleContent());
        date.put("articleCategories",findArticle.getArticleCategories());
        date.put("articleUrl",findArticle.getArticleUrl());

        return date;
    }

    @Override
    public JSONObject getArticlePartInfoByCategory(String categoriesName, int rows, int pageNum) {
        PageHelper.startPage(pageNum,rows);
        List<Article> articles;
        if ("".equals(categoriesName)){
            articles = articleMapper.getAllArticlePartInfo();
            categoriesName = "全部分类";
        }else {
            articles = articleMapper.getArticlePartInfoByCategory(categoriesName);
        }
        JSONArray jsonArray = new JSONArray();
        jsonArray = saveArticleToJsonArray(jsonArray,articles);

        PageInfo<Article> pageInfo = new PageInfo<>(articles);

        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getPageSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());

        JSONObject returnJson = new JSONObject();
        returnJson.put("status",200);
        returnJson.put("result",jsonArray);
        returnJson.put("category",categoriesName);
        returnJson.put("pageInfo",pageJson);

        return returnJson;

    }

    @Override
    public JSONObject getArticleManage(int rows, int currentPage) {
        PageHelper.startPage(currentPage,rows);
        List<Article> articles = articleMapper.getArticleManage();
        JSONArray jsonArray = new JSONArray();
        jsonArray = saveArticleToJsonArray(jsonArray,articles);

        PageInfo<Article> pageInfo = new PageInfo<>(articles);

        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getPageSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());

        JSONObject returnJson = new JSONObject();
        returnJson.put("status",200);
        returnJson.put("result",jsonArray);
        returnJson.put("pageInfo",pageJson);

        return returnJson;
    }

    @Override
    public JSONObject deleteArticleById(String id) {
        JSONObject jsonObject = new JSONObject();
        if (articleMapper.deleteArticleById(id)){
            jsonObject.put("status",200);
            jsonObject.put("message","成功删除");
        }else{
            jsonObject.put("status",403);
            jsonObject.put("message","删除失败");
        }
        return jsonObject;
    }

    @Override
    public boolean updateUrlByTime(Article article) {
        return articleMapper.updateUrlByTime(article);
    }

    @Override
    public JSONObject updateArticleByID(Article article) {
        JSONObject jsonObject = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String writeTime = sdf.format(new Date());
        article.setWriteTime(writeTime);
        boolean flag = articleMapper.updateArticleByID(article);
        if (flag){
            jsonObject.put("status",200);
            jsonObject.put("message","更新成功");
            jsonObject.put("articleTitle",article.getTitle());
            jsonObject.put("writeTime",article.getWriteTime());
            jsonObject.put("articleUrl","/findArticle?articleID=" + article.getId());
        }else {
            jsonObject.put("status",500);
            jsonObject.put("message","更新失败");
        }
        return jsonObject;
    }

    private JSONArray saveArticleToJsonArray(JSONArray jsonArray,List<Article> articles){
            JSONObject jsonObject;
            for (Article article :articles){
                jsonObject = new JSONObject();
                jsonObject.put("articleID",article.getId());
                jsonObject.put("articleCategories",article.getArticleCategories());
                jsonObject.put("articleTitle",article.getTitle());
                jsonObject.put("publishDate",article.getWriteTime());
                jsonArray.add(jsonObject);
            }
            return jsonArray;
    }

    @Override
    public JSONObject getAllArticleDesc(int rows,int pageNum){
        PageHelper.startPage(pageNum,rows);
        List<Article> articles = articleMapper.findAllArticleDesc();
        JSONArray array = new JSONArray();

        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        JSONObject pageJson = new JSONObject();
        pageJson.put("pageNum",pageInfo.getPageNum());
        pageJson.put("pageSize",pageInfo.getPageSize());
        pageJson.put("total",pageInfo.getTotal());
        pageJson.put("pages",pageInfo.getPages());
        pageJson.put("isFirstPage",pageInfo.isIsFirstPage());
        pageJson.put("isLastPage",pageInfo.isIsLastPage());
        for (Article article : articles){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",article.getId());
            jsonObject.put("title",article.getTitle());
            jsonObject.put("summary",article.getSummary());
            jsonObject.put("articleCategories",article.getArticleCategories());
            jsonObject.put("writeTime",article.getWriteTime());
            array.add(jsonObject);
        }
        if (array!=null){
            JSONObject returnJson = new JSONObject();
            returnJson.put("status",200);
            returnJson.put("result",array);
            returnJson.put("pageInfo",pageJson);
            return returnJson;
        }else {
            JSONObject returnJson = new JSONObject();
            returnJson.put("status",404);
            returnJson.put("message","获取文章信息失败");
            return returnJson;
        }

    }
}
